//
//  JTHUD.swift
//  TestUIKit
//
//  Created by Kiyo on 2021/4/20.
//

import Foundation
import UIKit

public final class JTHUD {
    
    private static var maskBgView: UIView = {
        let v = UIView(frame: UIScreen.main.bounds)
        v.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        v.alpha = 0
        return v
    }()
}

extension JTHUD {
    
    /// 显示菊花加载Loading
    /// - Parameter text: Loaing提示文案
    public static func showLoading(_ text: String? = nil) {
        
        guard let window = UIApplication.shared.keyWindow else { return }

        window.addSubview(maskBgView)
        
        UIView.animate(withDuration: 0.2) {
            maskBgView.alpha = 1
        }
        
        let v = JTLoadingView(text: text)
        window.showToast(v, duration: Double.greatestFiniteMagnitude, position: .center) { finish in
            maskBgView.removeFromSuperview()
        }
    }
    
    /// 显示吐司提示(纯文字)
    /// - Parameter text: 提示文案
    public static func showText(_ text: String) {
        
        guard let window = UIApplication.shared.keyWindow else { return }

        window.makeToast(text, position: .center)
    }
    
    /// 显示吐司提示(图文)
    /// - Parameters:
    ///   - image: 提示图片
    ///   - text: 提示文案
    public static func showImageAndText(image: UIImage, text: String) {
        
        guard let window = UIApplication.shared.keyWindow else { return }

        window.makeToast(text, position: .center, image: image)
    }
    
    /// 隐藏吐司
    public static func hideAll() {
        
        guard let window = UIApplication.shared.keyWindow else { return }
        
        maskBgView.removeFromSuperview()
        window.hideAllToasts()
    }
}
